import javax.swing.SwingUtilities;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField = null;
	private JTextField jTextField1 = null;
	private JButton jButton1 = null;
	protected Module module;
	private JTextArea jTextArea = null;
	protected int fileSize;
	protected int connectionLimit;
	protected int port = -1;
	private JLabel jLabel3 = null;
	private JTextField jTextField2 = null;
	private JButton jButton = null;
	private JScrollPane jScrollPane = null;
	protected JFileChooser fc = new JFileChooser();;

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(108, 45, 298, 19));
			jTextField.setText("localhost");
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(108, 72, 298, 19));
			jTextField1.setText("8080");
		}
		return jTextField1;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(9, 153, 397, 28));
			jButton1.setText("Send");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (fc.getSelectedFile() == null) {
						JOptionPane.showMessageDialog(null,
								"Please select a file", "Select A File",
								JOptionPane.ERROR_MESSAGE, null);
						fc.showOpenDialog(null);
					} else {
						module.send(jTextField.getText(), Integer
								.parseInt(jTextField1.getText()), fc
								.getSelectedFile());
					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}

	/**
	 * This method initializes jTextField2
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setBounds(new Rectangle(9, 126, 307, 19));
			jTextField2.setEditable(false);
		}
		return jTextField2;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(324, 126, 82, 19));
			jButton.setText("Browse");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					fc.showOpenDialog(null);
					jTextField2.setText(fc.getSelectedFile().getAbsolutePath());
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(414, 9, 397, 172));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GUI thisClass = new GUI();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public GUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		while (port < 0) {
			String str = JOptionPane
			.showInputDialog(null,
					"Please enter port number you want to accept incoming connections:");
			if (!str.isEmpty()) {
				port = Integer.parseInt(str);
			}
		}
		Module.setScreen(getJTextArea());
		module = new Module(port);
		module.run();
		this.setSize(836, 232);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(9, 99, 397, 19));
			jLabel3.setText("Select A File:");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(9, 72, 100, 19));
			jLabel2.setText("Receiver's Port:");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(9, 45, 100, 19));
			jLabel1.setText("Receiver's IP:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(9, 9, 397, 28));
			jLabel.setText("P2P Application");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(getJTextField1(), null);
			jContentPane.add(getJButton1(), null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(getJTextField2(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJScrollPane(), null);
		}
		return jContentPane;
	}

} // @jve:decl-index=0:visual-constraint="10,8"
